package eu.drseus.qiwidrone.prototype.horizon;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.TypedValue;
import android.view.View;
import eu.drseus.qiwidrone.prototype.R;

public class ArtificialHorizon {
	private View view;
	private Context context;
	
	private int totalWidthDIP = 204;
	private int totalHeightDIP = 204;
	
	private int originX = 0;
	private int originY = 0;
	
	private float roll = 0;
	private float pitch = 0;
	
	private Paint linePaint;

	private Bitmap bitmapArtificialHorizonOverlay;
	
	public ArtificialHorizon(Context context, View view) {
		this.context = context;
		this.view = view;
		imageLoad();
		initColors();
	}
	
	private void imageLoad() {
		Bitmap overlay = BitmapFactory.decodeResource(view.getResources(), R.drawable.artificial_horizon_trans_svg);
        bitmapArtificialHorizonOverlay = Bitmap.createScaledBitmap(overlay, (int) (convertDIPtoPX(totalWidthDIP)), (int) (convertDIPtoPX(totalHeightDIP)), true);
	}
	

	private void initColors() {
        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setColor(Color.DKGRAY);
        linePaint.setStrokeWidth(convertDIPtoPX(2));
	}
	
    
    // =========================================
    // Public Methods 
    // =========================================
	

	/**
	 * Sets the roll of the artificial horizon
	 * @param roll
	 */
	public void setRoll(float roll){
		this.roll = roll;
	}
	
	/**
	 * Sets the pitch of the artificial horizon
	 * @param pitch
	 */
	public void setPitch(float pitch){
		this.pitch = pitch;
	}
	
	public int getWidth(){
		return (int) convertDIPtoPX(totalWidthDIP);
	}
	
	public int getHeight(){
		return (int) convertDIPtoPX(totalHeightDIP);
	}
	
	public void setOrigin(int x, int y){
		this.originX = x;
		this.originY = y;
	}
	
	public void draw(Canvas canvas){

		

		//Draw Lines
		Paint paintLine = new Paint();
		paintLine.setColor(Color.BLACK);
		paintLine.setStrokeWidth(1);

		double dvH = ((double)getHeight()/100.0);
		double dvW = ((double)getWidth()/100.0);
		
		canvas.drawLine(originX, (int) (originY+(dvH*(100.0-(int)(pitch)))), (originX+getWidth()), (int) (originY+(dvH*(100.0-(int)(pitch)))), paintLine);
		
		canvas.drawLine((int) (originX+(dvW*(100.0-(int)(roll)))), originY, (int) (originX+(dvW*(100.0-(int)(roll)))), (originY+getHeight()), paintLine);
		
		//Draw Overlay
		canvas.drawBitmap(bitmapArtificialHorizonOverlay, originX, originY, null);
		
		//Debug Stuff
		Paint paintFont = new Paint(Paint.ANTI_ALIAS_FLAG);
		paintFont.setColor(Color.BLACK);
		paintFont.setStyle(Paint.Style.FILL);
		paintFont.setAntiAlias(true);
		paintFont.setTextSize(10);
		canvas.drawText("Roll: "+((int)roll)+" Pitch: "+((int)pitch), originX, originY-30, paintFont);
	
		canvas.save();
	}

	// =========================================
    // Basic Functionality 
    // =========================================
	
	private float convertDIPtoPX(int dip){
		return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, context.getResources().getDisplayMetrics());
	}
}
